﻿using System;
using CourseManager.Model.Data;
using Ext.Net;
using System.Linq;

namespace CourseManager.View.Contents.Admin
{
	public partial class UserContent : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				UserStore.DataSource = (from user in new DBEntity().Tab_User select user);
				UserStore.DataBind();
			}
		}

		[DirectMethod]
		public void ShowUpdateWindow(string id, string name, string type, string sex, string dept)
		{
			txtUpdateUserId.Text = id;
			txtUpdateName.Text = name;
			txtUpdateDept.Text = dept;
			cbUpdateSex.Text = sex;
			cbUpdateAccountType.Text = type;
			UpdateWindow.Show();
		}

		[DirectMethod]
		public void DeleteUser(string id)
		{
//			var entities = new CMDBEntities();
//			var user = (from u in entities.Tab_User where u.id == id select u).FirstOrDefault();
//			if (user == null) return;
//			Util.AddLog(Session["UserId"], "Delete User {id = " + user.id + "}");
//			entities.DeleteObject(user);
//			entities.SaveChanges();
//			Response.Redirect(Request.RawUrl);
		}

		protected void BtnAddOkClick(object sender, DirectEventArgs e)
		{
//			var entities = new CMDBEntities();
//			var user = new Tab_User
//			           	{
//			           		id = txtAddUserId.Text.Trim(), 
//							name = txtAddName.Text.Trim(),
//							password = Util.EncodeMD5(txtAddUserId.Text.Trim()),
//							sex = cbAddSex.Text,
//							accountType = AccountType.Parse(cbAddType.SelectedItem.Value)
//			           	};
//			entities.AddToTab_User(user);
//			entities.SaveChanges();
//			Util.AddLog(Session["UserId"], "Add User {id = " + user.id + "}");
//			Response.Redirect(Request.RawUrl);
		}

		protected void BtnUpdateOkClick(object sender, DirectEventArgs e)
		{
//			var entities = new CMDBEntities();
//			var user = (from u in entities.Tab_User where u.id == txtUpdateUserId.Text select u).FirstOrDefault();
//			user.sex = cbUpdateSex.Text.Trim();
//			user.name = txtUpdateName.Text.Trim();
//			user.dept = txtUpdateDept.Text.Trim();
//			user.accountType = AccountType.Parse(cbUpdateAccountType.SelectedItem.Value);
//			entities.SaveChanges();
//			Util.AddLog(Session["UserId"], "Update User {id = " + user.id + "}");
//			Response.Redirect(Request.RawUrl);
		}

		protected void BtnSearchClick(object sender, DirectEventArgs e)
		{
			var keyword = txtKeyword.Text.Trim();

//			if(keyword.Length == 0)
//			{
//				UserStore.DataSource = (from user in new CMDBEntities().Tab_User select user);
//			}
//			else
//			{
//				UserStore.DataSource = (from user in new CMDBEntities().Tab_User
//										where user.id.Contains(keyword) || user.name.Contains(keyword)
//												|| user.sex.Contains(keyword) || user.dept.Contains(keyword)
//										select user);
//				Util.AddLog(Session["UserId"], "Search User {Keyword = " + keyword + "}");
//			}
			pagingBar.SetPageIndex(1);
			UserStore.DataBind();
		}
	}
}